package com.lutzyt.delivery.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutzyt.delivery.bean.*;
import com.lutzyt.delivery.bean.req.Action;
import com.lutzyt.delivery.service.*;
import com.lutzyt.delivery.service.impl.CommodityCommentServiceImpl;
import com.lutzyt.delivery.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * controller 控制器
 *
 * @author zyt
 * @date 2021-05-18
 */
@Slf4j
@RestController
@RequestMapping("/api/customer")
public class CustomerController extends ActionController<CustomerService> {
  private CustomerService service;
  private DiscountCouponService discountCouponService;
  private OrderService orderService;
  private AddressService addressService;
  private OrderCommentService orderCommentService;
  private CommodityCommentService commodityCommentService;
  private HorsemanCommentService horsemanCommentService;

  @PostMapping("/init")
  public Result<Void> init(@RequestBody Customer customer) {
    log.info("customer:{}",customer);
    customer.setStatus(1);
    service.updateById(customer);
    return Result.success();
  }

  @GetMapping("/{id}")
  public Result<Customer> getById(@PathVariable Integer id) {
    final Customer bean = service.findById(id);
    return Result.success(bean);
  }

  @GetMapping("/{id}/discount/ids")
  public Result<List<String>> discountCouponIds(@PathVariable Integer id){
    List<String> ids = discountCouponService.findIdsByCustomerId(id);
    return Result.success(ids);
  }

  @GetMapping("/{id}/discount")
  public Result<List<DiscountCoupon>> discountCoupon(@PathVariable Integer id){
    List<DiscountCoupon> ids = discountCouponService.findByCustomerId(id);
    return Result.success(ids);
  }

  @GetMapping("/{id}/horsemanComment")
  public Result<List<HorsemanComment>> horsemanComment(@PathVariable Integer id){
    List<HorsemanComment> comments = horsemanCommentService.findByCustomerId(id);
    return Result.success(comments);
  }

  @GetMapping("/{id}/commodityComment")
  public Result<List<CommodityComment>> commodityComment(@PathVariable Integer id){
    List<CommodityComment> comments = commodityCommentService.findByCustomerId(id);
    return Result.success(comments);
  }

  @GetMapping("/{id}/orderComment")
  public Result<List<OrderComment>> orderComment(@PathVariable Integer id){
    List<OrderComment> comments = orderCommentService.findByCustomerId(id);
    return Result.success(comments);
  }


  @GetMapping("/{id}/discount/unused")
  public Result<List<DiscountCoupon>> unusedDiscountCoupon(@PathVariable Integer id){
    List<DiscountCoupon> discountCoupons = discountCouponService.findUnusedByCustomerId(id);
    return Result.success(discountCoupons);
  }

  @GetMapping("/{id}/order")
  public Result<List<Order>> order(@PathVariable Integer id){
    List<Order> orders = orderService.findByCustomerId(id);
    return Result.success(orders);
  }

  @GetMapping("/{id}/address")
  public Result<List<Address>> address(@PathVariable Integer id){
    List<Address> ids = addressService.findByCustomerId(id);
    return Result.success(ids);
  }

  @PutMapping("/{id}")
  public Result<Void> update(@RequestBody Customer bean) {
    log.info("更新菜单:{}", bean);
    service.update(bean);
    return Result.success();
  }

  @PostMapping("/page")
  public Result<Page<Customer>> page(Integer pageSize, Integer pageNum) {
    final Page<Customer> page = service.page(new Page<>(pageNum, pageSize));
    return Result.success(page);
  }

  @DeleteMapping("/{id}")
  public Result<Void> del(@PathVariable Integer id) {
    log.info("删除:{}", id);
    service.deleteById(id);
    return Result.success();
  }

  @DeleteMapping
  public Result<Void> del(@RequestParam("id") List<String> ids) {
    log.info("pl删除:{}", ids);
    service.removeByIds(ids);
    return Result.success();
  }

  @PostMapping
  public Result<Void> insert(@RequestBody Customer bean) {
    log.info("添加菜单:{}", bean);
    service.insert(bean);
    return Result.success();
  }

  @Autowired
  public void setOrderCommentService(OrderCommentService orderCommentService) {
    this.orderCommentService = orderCommentService;
  }

  @Autowired
  public void setCommodityCommentService(CommodityCommentService commodityCommentService) {
    this.commodityCommentService = commodityCommentService;
  }

  @Autowired
  public void setHorsemanCommentService(HorsemanCommentService horsemanCommentService) {
    this.horsemanCommentService = horsemanCommentService;
  }

  @Autowired
  public void setAddressService(AddressService addressService) {
    this.addressService = addressService;
  }

  @Autowired
  public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }

  @Autowired
  public void setDiscountCouponService(DiscountCouponService discountCouponService) {
    this.discountCouponService = discountCouponService;
  }

  @Autowired
  public void setService(CustomerService service) {
    this.service = service;
  }

}
    